package com.ruoyi.framework.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author qck
 * @desc mybatis拦截器
 * @data 2020/12/28 16:01
 */
@Slf4j
@Component
public class MetaHandler implements MetaObjectHandler {
//    @Autowired
//    private AuthenticationTrustResolver authenticationTrustResolver;
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("come to insert fill .........");
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if(!authenticationTrustResolver.isAnonymous(authentication) && authentication!=null){
//            AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal();
//            this.setFieldValByName("createUser", user.getUsername(), metaObject);
//            this.setFieldValByName("updateUser",  user.getUsername(), metaObject);
//        }else{
//            this.setFieldValByName("createUser", "unknown", metaObject);
//            this.setFieldValByName("updateUser",  "unknown", metaObject);
//        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("come to update fill .........");
        this.setFieldValByName("update_time", LocalDateTime.now(), metaObject);
//        this.setFieldValByName("version", "version+1", metaObject);
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if(!authenticationTrustResolver.isAnonymous(authentication) && authentication!=null){
//            AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal();
//            this.setFieldValByName("updateUser",  user.getUsername(), metaObject);
//        }else{
//            this.setFieldValByName("updateUser",  "unknown", metaObject);
//        }
    }

}
